<template>
  <div
    class="skeleton"
    :class="{ animated }"
    :style="{ width, height, 'background-color': bgColor }"
  >
    <!-- 盒子 -->
    <div class="block"></div>
    <!-- 伪元素闪动动画 -->
  </div>
</template>

<script>
export default {
  name: 'Skeleton',
  props: {
    bgColor: {
      type: String,
      default: 'rgba(255, 255, 255, 0.1)'
    },
    width: {
      type: String,
      default: '100px'
    },
    height: {
      type: String,
      default: '50px'
    },
    animated: {
      type: Boolean,
      default: true
    }
  }
}
</script>


<style lang="less" scoped>
.skeleton {
  display: inline-block;
  position: relative;
  overflow: hidden;

  .block {
    width: 100%;
    height: 100%;
    border-radius: 2px;
  }
}

.animated {
  &::after {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    width: 50%;
    height: 100%;
    background: linear-gradient(
      to left,
      rgba(255, 255, 255, 0) 0,
      rgba(255, 255, 255, 0.3) 50%,
      rgba(255, 255, 255, 0) 100%
    );
    transform: skewX(-45deg);
    animation: shan 1.5s ease 0s infinite;
  }
}

@keyframes shan {
  0% {
    left: -100%;
  }
  100% {
    left: 120%;
  }
}
</style>